package cms.mager.dao;

import cms.common.PageData;
import cms.dao.BaseDao;
import cms.mager.domain.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能: 用户数据表操作类
 * Author: yuhui
 * Date: 2021/4/8 16:06
 */
public class UserDao extends BaseDao {
    /**
    * 功能描述: 用户登录查询
    * @Author: yuhui  2021/4/8 16:16
    */
    public User findOne(String name, String pwd){
        String sql = "select id,username,password,realname,role,deleted,img,create_time,modify_time,deleted_time" +
                " from user where username=? and password=?";
        String[] param = {name,pwd};
        List<User> one = findOne(sql, User.class, param);
        return one.size()!=0 ? one.get(0) : null;
    }

    /**
    * 功能描述: 展示所有用户信息
    * @Author: yuhui  2021/4/8 16:17
    */
    public List<User> findAll(){
        String sql = "select id,username,password,realname,role,deleted,img,create_time,modify_time,deleted_time from user";
        List<User> all = findAll(sql, User.class, null);
        return all;
    }

    /**
    * 功能描述: 模糊查询员工信息
    * @Author: yuhui  2021/4/8 16:19
    */
    public List<User> findUserByLike(String name,Integer role,Integer del){
        String sql = "select id,username,password,realname,role,deleted,img,create_time,modify_time,deleted_time from user where 1=1";
        List<Object> param = new ArrayList<>();
        if(name != "" && name != null){
            sql = sql+" and username = ?";
            param.add(name);
        }else if(role != null){
            sql = sql + " and role = ?";
            param.add(role);
        }else if(del != null){
            sql = sql + " and deleted = ?";
            param.add(del);
        }
        return findAll(sql,User.class,param.toArray());
    }

    /**
    * 功能描述: 分页查询数据接口
    * @Author: yuhui  2021/4/8 16:38
    */
    public PageData<User> findUserPage(int page, int pageSize, PageData pd){
        String sql_page = "select id,username,password,realname,role,deleted,img,create_time,modify_time,deleted_time" +
                " from user limit ?,?";
        String sql_count = "select count(1) from user";
        page = (page-1)*pageSize;
        Object[] param = {page,pageSize};
        return findPage(sql_count,sql_page,User.class,param,pd);
    }

    public boolean addData(Object[] objArr){
        String sql = "insert into user(username,password,realname,role,create_time) values(?,?,?,?,?)";
        return executeUpdate(sql,objArr);
    }

    /**
    * 功能描述: 重置用户密码
    * @Author: yuhui  2021/4/9 19:23
    */
    public boolean reset(int id,String pwd){
        String sql = "update user set password=?,modify_time=? where id=?";
        Object[] obj = {pwd,new Date(),id};
        return executeUpdate(sql,obj);
    }


    /**
    * 功能描述: 删除用户
    * @Author: yuhui  2021/4/9 19:25
    */
    public boolean delUserById(int id) {
        Object[] obj = {new Date(),id};
        String sql = "update user set deleted = 2,deleted_time=? where id = ?";
        return executeUpdate(sql,obj);
    }

    /**
    * 功能描述: 位下拉框查询所有业务员
    * @Author: yuhui  2021/4/10 16:45
    */
    public List<User> getAllName(){
        String sql = "select id,username from user where role = 1";
        return findAll(sql,User.class,null);
    }

    public boolean updImg(Integer id, String imgFile) {
        String sql = "update user set img=? where id=?";
        Object[] param = {imgFile,id};
        return executeUpdate(sql,param);
    }
}
